package theOrbalEra.client.gui;

import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;

import org.lwjgl.opengl.GL11;

import theOrbalEra.common.TheOrbalEra;
import theOrbalEra.common.block.ContainerOrballFactory;
import theOrbalEra.common.block.TileEntityOrballFactory;
import theOrbalEra.common.item.OrbalItemEleBag;
import theOrbalEra.common.item.OrbalItemElement;
import theOrbalEra.common.item.OrbalItemOrbment;
import theOrbalEra.common.network.PacketDeBag;
import theOrbalEra.common.network.PacketDeEle;
import theOrbalEra.common.network.PacketSetOpen;
import theOrbalEra.common.util.DefultValue;
import theOrbalEra.common.util.OrbalUtils;
import theOrbalEra.common.util.GetName;


public class GuiOrbalFactory extends GuiContainer {
	private final TileEntityOrballFactory tile;
	private final InventoryPlayer inventoryPlayer;

	public GuiOrbalFactory(InventoryPlayer inventoryPlayer,
			TileEntityOrballFactory tileEntity) {
		super(new ContainerOrballFactory(inventoryPlayer, tileEntity));
		tile = tileEntity;
		this.inventoryPlayer=inventoryPlayer;
		doesGuiPauseGame();
		ySize = 255;

	}

	@Override
	protected void drawGuiContainerBackgroundLayer(float var1, int var2,
			int var3) {

		GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
		mc.renderEngine.bindTexture(DefultValue.Gui_OrbalFactory);
		final int var5 = (width - xSize) / 2;
		final int var6 = (height - ySize) / 2;
		drawTexturedModalRect(var5, var6, 0, 0, xSize, ySize);
		final int var7=176;
		//draw overlay in orbment
		if (!tile.getPoint(0)||tile.OrbalFacCon[0]==null||!(tile.OrbalFacCon[0].getItem() instanceof OrbalItemOrbment)) {
			drawTexturedModalRect(var5+79, var6 + 72, var7+16, 0,15, 16);
		}
			
			if (!tile.getPoint(1)) {
				drawTexturedModalRect(var5+27, var6 + 101, var7+16, 0,15, 16);
	}
			if (!tile.getPoint(2)) {
				drawTexturedModalRect(var5+27, var6 + 39, var7+16, 0,15, 16);
	}
			if (!tile.getPoint(3)) {
				drawTexturedModalRect(var5+79, var6 + 10, var7+16, 0,15, 16);
	}
			if (!tile.getPoint(4)) {
				drawTexturedModalRect(var5+131, var6 + 101, var7+16, 0,15, 16);
	}
			if (!tile.getPoint(5)) {
				drawTexturedModalRect(var5+131, var6 + 39, var7+16, 0,15, 16);
	}
			
      //draw lock/unlock
			if (tile.OrbalFacCon[0]!=null&&(tile.OrbalFacCon[0].getItem() instanceof OrbalItemOrbment)&&tile.getPoint(0)) {
				drawTexturedModalRect(var5+37, var6 + 126, var7, 0,15, 15);
				}
			for(int i=1;i<6;i++){
				if (tile.getPoint(i)) {
					drawTexturedModalRect(var5+37+i*16, var6 + 126, var7, 0,15, 15);
				}
			}
	  //draw line	
		if(tile.getLine(0)){
			drawTexturedModalRect(var5+45, var6 + 78,var7, 20,30, 34);
		}
		if(tile.getLine(1)){
			drawTexturedModalRect(var5+32, var6 + 62, var7+48, 22,4, 34);
		}
		if(tile.getLine(2)){
			drawTexturedModalRect(var5+45, var6 + 17, var7, 21,30, 33);
		}
		if(tile.getLine(3)){
			drawTexturedModalRect(var5+97, var6 + 78, var7+31, 69,31, 33);
		}
		if(tile.getLine(4)){
			drawTexturedModalRect(var5+136, var6 + 62, var7+48, 22,4, 34);
			}
		
	}

	@Override
	protected void mouseClicked(int par1, int par2, int par3) {
		final int var1 = (width - xSize) / 2;
		final int var2 = (height - ySize) / 2+141;
		final int var3 = (height - ySize) / 2+126;
		int[]have={0,0,0,0,0,0,0};
		int[] eleInInv=OrbalItemElement.eleInInventory(inventoryPlayer.player);
		have=eleInInv;
		int[] openneeded={0,0,0,0,0,0,0};
		boolean[] enough={false,false,false,false,false,false,false};
		int slot;
		slot=distinguishSlot(par1,par2,var1, var2, var3);
		if(slot!=-1){
			
		if(tile.OrbalFacCon[0]!=null&&(tile.OrbalFacCon[0].getItem() instanceof OrbalItemOrbment)&&!tile.getPoint(slot)){
		openneeded=OrbalUtils.openneeded(slot);
		if(OrbalItemEleBag.hasbag(inventoryPlayer.player)){
			ItemStack[] main=inventoryPlayer.mainInventory;
			 int wei=0;
			
			for(int i=0;i<inventoryPlayer.mainInventory.length;i++){
				if(main[i]!=null&&main[i].itemID==TheOrbalEra.ele_bagId+256){
					wei=i;
					break;
					}
				}
			int[] bag = OrbalItemEleBag.getEleCount(inventoryPlayer.mainInventory[wei]);

			for(int i=0;i<7;i++){
			have[i]+=bag[i];
			}
		}
		
			if(have[0]-openneeded[0]>=0) {enough[0]=true;}
			if(have[1]-openneeded[1]>=0) {enough[1]=true;}
			if(have[2]-openneeded[2]>=0) {enough[2]=true;}
			if(have[3]-openneeded[3]>=0) {enough[3]=true;}
			if(have[4]-openneeded[4]>=0) {enough[4]=true;}
			if(have[5]-openneeded[5]>=0) {enough[5]=true;}
			if(have[6]-openneeded[6]>=0) {enough[6]=true;}
		if(enough[0]&&enough[1]&&enough[2]&&enough[3]&&enough[4]&&enough[5]&&enough[6]){
			int[]cao=OrbalItemOrbment.getCao(tile.OrbalFacCon[0]);
			cao[slot]=1;
			if(OrbalItemEleBag.hasbag(inventoryPlayer.player)){
				int wei=OrbalItemEleBag.getBaginInv(inventoryPlayer.player);
				int[] bag = OrbalItemEleBag.getEleCount(inventoryPlayer.mainInventory[wei]);
				
				for(int i=0;i<bag.length;i++){
					if(bag[i]-openneeded[i]<=0){
						openneeded[i]-=bag[i];
						bag[i]=0;
					}
					else{
						bag[i]-=openneeded[i];
						openneeded[i]=0;
					}
					
				}
				PacketDeBag.deBag(wei, bag);
				
			}
		
				deStack(openneeded[0],openneeded[1],openneeded[2],openneeded[3],openneeded[4],openneeded[5],openneeded[6]);
			
			OrbalItemOrbment.setCao(tile.OrbalFacCon[0],cao);
			PacketSetOpen.setOpen(tile.xCoord, tile.yCoord, tile.zCoord, cao);
		}
		else{
			inventoryPlayer.player.addChatMessage(GetName.localize("player.insufficientBalance"));
		}
			
		
		}
		}
	
		super.mouseClicked(par1, par2, par3);


	


}

	private void deStack(int fire, int water, int earth, int wind,int time,int space,int unreal) {
		
			PacketDeEle.deEle(fire, water, earth, wind,time,space,unreal);
			
			
	}

	
	//to confirm which slot
	private int distinguishSlot(int par1,int par2,int var1,int var2,int var3){
		if(par1>=37+16*0+var1&&par1<=52+16*0+var1&&par2<=var2&&par2>=var3){
			return 0;
			}
		if(par1>=37+16*1+var1&&par1<=52+16*1+var1&&par2<=var2&&par2>=var3){
			return 1;
		}
		if(par1>=37+16*2+var1&&par1<=52+16*2+var1&&par2<=var2&&par2>=var3){
			return 2;
		}
		if(par1>=37+16*3+var1&&par1<=52+16*3+var1&&par2<=var2&&par2>=var3)
		{
			return 3;
		}
		if(par1>=37+16*4+var1&&par1<=52+16*4+var1&&par2<=var2&&par2>=var3){
			return 4;
		}
		if(par1>=37+16*5+var1&&par1<=52+16*5+var1&&par2<=var2&&par2>=var3){
			return 5;
		}
		return -1;
		}
}
